package cn.bs.busuo.front.content.service.impl;

import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.front.content.dao.cache.UserHotelArticleCacheRepository;
import cn.bs.busuo.front.content.dao.persist.repository.UserHotelArticle;
import cn.bs.busuo.front.content.pojo.entity.ContentHotelArticle;
import cn.bs.busuo.front.content.pojo.entity.ContentHotelArticleDetail;
import cn.bs.busuo.front.content.pojo.param.HotelArticleAddNewParam;
import cn.bs.busuo.front.content.pojo.param.HotelArticleUpDateParam;
import cn.bs.busuo.front.content.pojo.vo.ContentHotelArticleByTitleVO;
import cn.bs.busuo.front.content.pojo.vo.ContentHotelArticleDetailByIdVO;
import cn.bs.busuo.front.content.pojo.vo.UserContentHotelArticleByIdVO;
import cn.bs.busuo.front.content.service.UserHotelArticleService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Feng
 * @since: 2023/12/8
 */
@Service
public class UserHotelArticleServiceImpl implements UserHotelArticleService {

    @Autowired
    private UserHotelArticle userHotelArticle;
    @Autowired
    private UserHotelArticleCacheRepository userHotelArticleCacheRepository;

    //发布文章
    @Override
    public void addNewHotelArticle(CurrentPrincipal currentPrincipal, HotelArticleAddNewParam hotelArticleAddNewParam, Long id) {
        //插入到酒店文章
        ContentHotelArticle contentHotelArticle = new ContentHotelArticle();
        contentHotelArticle.setBrief(hotelArticleAddNewParam.getBrief());
        contentHotelArticle.setCoverUrl(hotelArticleAddNewParam.getCoverUrl());
        contentHotelArticle.setHotelsId(id);
        contentHotelArticle.setKeywords(hotelArticleAddNewParam.getKeywords());
        String title = hotelArticleAddNewParam.getTitle();
        contentHotelArticle.setTitle(title);

        //插入当事人信息
        contentHotelArticle.setAuthorId(currentPrincipal.getId());
        contentHotelArticle.setAuthorName(currentPrincipal.getUsername());

        //插入到酒店详情实体类
        ContentHotelArticleDetail contentHotelArticleDetail = new ContentHotelArticleDetail();
        contentHotelArticleDetail.setDetail(hotelArticleAddNewParam.getDetail());

        //调用添加方法
        addHotelArticle(contentHotelArticleDetail, contentHotelArticle,title);


    }

    //用户删除文章,但是只是设置为不显示
    @Override
    public void deleteArticle(CurrentPrincipal currentPrincipal, Long id) {
        //判断该文章是否存在
        ContentHotelArticle contentHotelArticle = userHotelArticle.selectHotelArticleById(id);
        if (contentHotelArticle == null) {
            String msg = "该文章不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, msg);
        }
        //判断文章作者是否是当前登陆人员
        if (!currentPrincipal.getUsername().equals(contentHotelArticle.getAuthorName())) {
            String msg = "该文章不是您发布的";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, msg);
        }
        userHotelArticle.updateHotelArticleById(id);
        //在redis里面修改文章状态
        userHotelArticleCacheRepository.deleteArticle(id);
    }


    //根据酒店Id查询文章
    @Override
    public String selectHotelArticleByHotelId(Long id) {
        List<UserContentHotelArticleByIdVO> userContentHotelArticleByIdVO = userHotelArticle.selectHotelArticleByHotelId(id);
        if (userContentHotelArticleByIdVO.size() == 0) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该酒店没有文章");
        }
        String json = JSON.toJSON(userContentHotelArticleByIdVO).toString();
        return json;
    }

    //查看酒店文章详情
    @Override
    public ContentHotelArticleDetailByIdVO selectHotelArticleDetailById(Long id) {
        ContentHotelArticleDetailByIdVO contentHotelArticleDetailByIdVO = userHotelArticle.selectHotelArticleDetailById(id);
        if (contentHotelArticleDetailByIdVO == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "没有这篇文章");
        }
        //增加浏览量
        userHotelArticle.increaseClickCount(id);
        return contentHotelArticleDetailByIdVO;
    }


    //查询酒店文章详情并好评
    @Override
    public void increaseUpCount(Long id) {
        ContentHotelArticleDetailByIdVO contentHotelArticleDetailByIdVO = userHotelArticle.selectHotelArticleDetailById(id);
        if (contentHotelArticleDetailByIdVO == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "没有这篇文章");
        }
        //点赞
        int i = userHotelArticle.increaseUpCount(id);
        if (i != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "点赞失败");
        }
    }

    //查询酒店文章详情并差评
    @Override
    public void increaseDownCount(Long id) {
        ContentHotelArticleDetailByIdVO contentHotelArticleDetailByIdVO = userHotelArticle.selectHotelArticleDetailById(id);
        if (contentHotelArticleDetailByIdVO == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "没有这篇文章");
        }
        //差评
        int i = userHotelArticle.increaseDownCount(id);
        if (i != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "点赞失败");
        }
    }


    //用户查询自己酒店文章
    @Override
    public String selectUserListHotelArticle(String authorName) {
        List<UserContentHotelArticleByIdVO> userListHotelArticle = userHotelArticle.selectUserListHotelArticle(authorName);
        if (userListHotelArticle.size() == 0) {
            throw new ServiceException(ServiceCode.OK, "没有文章,快去写文章吧");
        }
        String json = JSON.toJSON(userListHotelArticle).toString();
        return json;
    }

    //修改酒店文章信息
    @Override
    public void upDateHotelArticle(String authorName, Long id, HotelArticleUpDateParam hotelArticleUpDateParam) {
        ContentHotelArticle contentHotelArticle = userHotelArticle.selectHotelArticleById(id);
        if (contentHotelArticle == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "没有这篇文章");
        }
        if (!authorName.equals(contentHotelArticle.getAuthorName())) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "很抱歉,这篇文章不是您写的");
        }
        //查询是否有详情
        ContentHotelArticleDetail contentHotelArticleDetail = userHotelArticle.selectHotelArticleDetail(id);
        if (contentHotelArticleDetail == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "没有该文章的详情");
        }
        //判断该文章标题是否重复
        int title = userHotelArticle.contentHotelArticleByTitle(hotelArticleUpDateParam.getTitle());
        if (title != 0) {
            String msg = "该文章标题已存在,请重新输入";
            throw new ServiceException(ServiceCode.ERROR_INSERT, msg);
        }


        //调用修改文章的IO方法
        upDataArticle(id, hotelArticleUpDateParam);
    }


    //添加酒店文章IO
    @Transactional
    public void addHotelArticle(ContentHotelArticleDetail contentHotelArticleDetail, ContentHotelArticle contentHotelArticle,String title) {
        //判断该文章标题是否重复
        int titles = userHotelArticle.contentHotelArticleByTitle(contentHotelArticle.getTitle());
        if (titles != 0) {
            String msg = "该文章标题已存在,请重新输入";
            throw new ServiceException(ServiceCode.ERROR_INSERT, msg);
        }
        //添加到酒店文章
        int i = userHotelArticle.addNewHotelArticle(contentHotelArticle);
        if (i != 1) {
            String msg = "发布酒店文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT, msg);
        }

        //存进redis酒店文章表
        ContentHotelArticleByTitleVO contentHotelArticleByTitleVO = userHotelArticle.selectHotelArticleByTitleVO(title);
        userHotelArticleCacheRepository.addNewHotelArticle(contentHotelArticleByTitleVO.getId(),contentHotelArticleByTitleVO);

        //从酒店文章里面获取文章Id
        Long NewArticleId = userHotelArticle.selectHotelArticleByTitle(contentHotelArticle.getTitle());
        contentHotelArticleDetail.setHotelArticleId(NewArticleId);

        //存进redis酒店文章详情表

        //添加到酒店文章详情
        int j = userHotelArticle.addNewHotelArticleDetail(contentHotelArticleDetail);
        if (j != 1) {
            String msg = "发布酒店文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT, msg);
        }

    }


    //修改文章的IO方法
    @Transactional
    public void upDataArticle(Long id, HotelArticleUpDateParam hotelArticleUpDateParam) {
        String newTitle = hotelArticleUpDateParam.getTitle();
        String newBrief = hotelArticleUpDateParam.getBrief();
        String newCoverUrl = hotelArticleUpDateParam.getCoverUrl();
        String newKeywords = hotelArticleUpDateParam.getKeywords();

        //修改文章表
        int i = userHotelArticle.updateHotelArticle(newTitle, newBrief, newCoverUrl, newKeywords, id);
        if (i != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "修改失败");
        }

        //修改详情表
        int j = userHotelArticle.updateHotelArticleDetail(hotelArticleUpDateParam.getDetail(), id);
        if (j != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "修改详情失败");
        }



    }


}
